package com.wenwei.sharebox.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.jaeger.library.StatusBarUtil;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.RefundBean;
import com.wenwei.sharebox.bean.RentBean;
import com.wenwei.sharebox.bean.UserShareBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;

/**
 * Created by Administrator on 2017/7/11.
 */
public class QRcodeActivity extends BaseActivity {

    private CaptureFragment captureFragment;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_qr_code;
    }
    public static boolean isOpen = false;
    @Override
    protected void initView() {
        super.initView();

//        StatusBarUtil.setTransparent(QRcodeActivity.this);
//        StatusBarUtil.setTranslucent(QRcodeActivity.this, 1);
        captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();


        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear1);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOpen) {
                    CodeUtils.isLightEnable(true);
                    isOpen = true;
                } else {
                    CodeUtils.isLightEnable(false);
                    isOpen = false;
                }

            }
        });

        LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linear2);
        linearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("qr_type",getIntent().getIntExtra("qr_type",0));
                if(getIntent().getIntExtra("qr_type",0) ==5){
                    //退款
                    bundle.putString("reason",getIntent().getStringExtra("reason"));
                    UserShareBean.DataBean bean = (UserShareBean.DataBean)getIntent().getSerializableExtra("bean");
                    bundle.putInt("orderId",bean.getId());
                }
                if(getIntent().getIntExtra("qr_type",0) == 4 ){
                    //归还
                    UserShareBean.DataBean bean = (UserShareBean.DataBean)getIntent().getSerializableExtra("bean");
                    bundle.putInt("orderId",bean.getId());
                }
                goActivity(mContext,InputCodeActivity.class,bundle);

            }
        });
    }


    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
//            Bundle bundle = new Bundle();
//            bundle.putString("deviceCode",result);
//            goActivity(mContext,ShareConfirmActivity.class,bundle);

            goToActivity(result);
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);

        }
    };


    private int mQrType;
    private void goToActivity(String deviceCode){
        mQrType = getIntent().getIntExtra("qr_type",0);
        Bundle bundle = new Bundle();
        bundle.putString("deviceCode",deviceCode);
        switch (mQrType){
            case Constant.QR_TYPE_CHECK:
                goActivity(mContext,ProvideCheckActivity.class,bundle);
                break;
            case Constant.QR_TYPE_REPLENISHMENT:
                goActivity(mContext,ReplenishmentActivity.class,bundle);
                break;
            case Constant.QR_TYPE_REPAIR:
                goActivity(mContext,RepairActivity.class,bundle);
                break;
            case Constant.QR_TYPE_RETURN:
                //申请退款
                returnMoney(deviceCode);
                break;
            case Constant.QR_TYPE_REFUND:
                //申请归还
                refundGoods(deviceCode);
                break;
        }
    }


    //申请退款

    private int orderId;
    private String reasonStr;
    private UserShareBean.DataBean bean;
    BaseSubscriber<RefundBean> refundSubscriber;
    private void returnMoney(String deviceNum){
        bean = (UserShareBean.DataBean)getIntent().getSerializableExtra("bean");
        orderId = bean.getId() ;
        reasonStr = getIntent().getStringExtra("reason");
        refundSubscriber = new BaseSubscriber<RefundBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(RefundBean refundBean) {
                goActivity(mContext,OperationSuccessActivity.class);
            }
        };
        ApiManager.getInstance().refund(refundSubscriber,orderId,deviceNum,reasonStr);
    }

    //申请归还
    private UserShareBean.DataBean beans;
    private BaseSubscriber<RentBean> rentSubscriber;
    private void refundGoods(String deviceNum){
        beans = (UserShareBean.DataBean)getIntent().getSerializableExtra("bean");;
        int orderId = beans.getId();
        rentSubscriber = new BaseSubscriber<RentBean>(mContext) {
            @Override
            public void onSuccess(RentBean rentBean) {
                goActivity(mContext,OperationSuccessActivity.class);
            }
        };
        ApiManager.getInstance().rent(rentSubscriber,orderId,deviceNum);
    }
}
